﻿/*!
 * @Description:agency中人员提示js
 * @Author:ZB,YPL
 * @Date:2014-04-10 
 */

var EmployeePrompt = {};

//根据输入内容获得员工智能提示返回员工id（不需要部门Id）
EmployeePrompt.getEmployeeByInput = function (txtInputId, txtHidId, exceptMe, hasPublicAccount, employeeDefaultV, hasFilterAccount) {
    var getUrl = function () {
        if (hasFilterAccount == undefined) {
            hasFilterAccount = "";
        }
        return "/property/user-auto-complete-obtain-ajax?name=" + escape($('#' + txtInputId).val()) + '&ExceptMe=' + exceptMe + '&hasPublicAccount=' + hasPublicAccount + '&hasFilterAccount=' + hasFilterAccount;
    };

    $('#' + txtInputId).change(function () {
        if (txtHidId) {
            $('#' + txtHidId).val('');
        }
    });

    if (employeeDefaultV) {
        var tempTxtEmployeeName = employeeDefaultV;
        if ($('#' + txtInputId).val() == tempTxtEmployeeName) {
            $('#' + txtInputId).css('color', 'gray');
        }
        $('#' + txtInputId).focus(function () {
            $('#' + txtInputId).css('color', '');
            if (tempTxtEmployeeName == $('#' + txtInputId).val()) {
                $('#' + txtInputId).val('');
            }
        }).blur(function () {
            if (!$('#' + txtInputId).val().replace(/[ ]/g, "")) {
                $('#' + txtInputId).val(tempTxtEmployeeName).css('color', 'gray');
            }
        });
    }

    $('#' + txtInputId).autocomplete(getUrl, {
        width: 260,
        max: 20,
        selectFirst: false,
        formatItem: function (row) {
            if (row.IsPublicAccount) {
                return row.UserName + "<span style='float:right'>" + row.DepartmentName + "</span>";
            } else {
                return row.UserName + "(" + row.EmployeeNo + ")<span style='float:right'>" + row.DepartmentName + "</span>";
            }
        },
        formatResult: function (row) {
            return row.UserName;
        }
    }).result(function (a, b) {
        $('#' + txtHidId).val(b.UserKeyId);
    });

};

//根据输入内容获得员工智能提示返回员工id和短号（不需要部门Id）hjx2016-04-20
EmployeePrompt.getIDAndCornetByInput = function (txtInputId, txtHidId, txtCornet, exceptMe, hasPublicAccount, employeeDefaultV, hasFilterAccount) {
    var getUrl = function () {
        if (hasFilterAccount == undefined) {
            hasFilterAccount = "";
        }
        return "/property/user-auto-complete-obtain-ajax?name=" + escape($('#' + txtInputId).val()) + '&ExceptMe=' + exceptMe + '&hasPublicAccount=' + hasPublicAccount + '&hasFilterAccount=' + hasFilterAccount;
    };

    $('#' + txtInputId).change(function () {
        if (txtHidId) {
            $('#' + txtHidId).val('');
        }
    });

    $('#' + txtCornet).change(function () {
        if (txtCornet) {
            $('#' + txtCornet).val('');
        }
    });

    if (employeeDefaultV) {
        var tempTxtEmployeeName = employeeDefaultV;
        if ($('#' + txtInputId).val() == tempTxtEmployeeName) {
            $('#' + txtInputId).css('color', 'gray');
        }
        $('#' + txtInputId).focus(function () {
            $('#' + txtInputId).css('color', '');
            if (tempTxtEmployeeName == $('#' + txtInputId).val()) {
                $('#' + txtInputId).val('');
            }
        }).blur(function () {
            if (!$('#' + txtInputId).val().replace(/[ ]/g, "")) {
                $('#' + txtInputId).val(tempTxtEmployeeName).css('color', 'gray');
            }
        });
    }

    $('#' + txtInputId).autocomplete(getUrl, {
        width: 260,
        max: 20,
        selectFirst: false,
        formatItem: function (row) {
            if (row.IsPublicAccount) {
                return row.UserName + "<span style='float:right'>" + row.DepartmentName + "</span>";
            } else {
                return row.UserName + "(" + row.EmployeeNo + ")<span style='float:right'>" + row.DepartmentName + "</span>";
            }
        },
        formatResult: function (row) {
            return row.UserName;
        }
    }).result(function (a, b) {
        $('#' + txtHidId).val(b.UserKeyId);
        $('#' + txtCornet).val(b.Cornet);
    });

};

//根据输入内容获得部门及员工智能提示返回部门Id及员工id
EmployeePrompt.getDepartAndEmployeeByInput = function (txtDepartInputId, txtDepartHidId, txtEmployeeInputId, txtEmployeeHidId, exceptMe, hasPublicAccount, scope, txtEmployeeValue, txtDepartValue, isDepartLink, isEmployeeLink, DeparControlEmployee, ReturnFun) {
    //赵贲改,选部门的时候,无需判断范围   
    var getDepartUrl = function () {  
        return "/property/department-auto-match-ajax?name=" + escape($('#' + txtDepartInputId).val()) + '&ExceptMe=' + exceptMe + '&hasPublicAccount=true&scope=' + scope;
    };
    var getEmployeetUrl = function () {
        if (DeparControlEmployee == undefined) {
            DeparControlEmployee = true;
        }
        if (txtDepartInputId && DeparControlEmployee) {
            return "/property/user-auto-complete-obtain-ajax?name=" + escape($('#' + txtEmployeeInputId).val()) + '&ExceptMe=' + exceptMe + '&hasPublicAccount=' + hasPublicAccount + '&DepartmentKeyId=' + $('#' + txtDepartHidId).val();
        } else {
            return "/property/user-auto-complete-obtain-ajax?name=" + escape($('#' + txtEmployeeInputId).val()) + '&ExceptMe=' + exceptMe + '&hasPublicAccount=' + hasPublicAccount;
        }
    };
    if (txtDepartValue) {
        var tempTxtDepartmentName = txtDepartValue;
        if ($('#' + txtDepartInputId).val() == tempTxtDepartmentName) {
            $('#' + txtDepartInputId).css('color', 'gray');
        }
        $('#' + txtDepartInputId).focus(function () {
            $('#' + txtDepartInputId).css('color', '');
            if (tempTxtDepartmentName == $('#' + txtDepartInputId).val()) {
                $('#' + txtDepartInputId).val('');
            }

        }).blur(function () {
            if (!$('#' + txtDepartInputId).val().replace(/[ ]/g, "")) {
                $('#' + txtDepartInputId).val(tempTxtDepartmentName).css('color', 'gray');
            }
        });
        var tempTxtEmployeeName = txtEmployeeValue;
        if ($('#' + txtEmployeeInputId).val() == tempTxtEmployeeName) {
            $('#' + txtEmployeeInputId).css('color', 'gray');
        }
        $('#' + txtEmployeeInputId).focus(function () {
            $('#' + txtEmployeeInputId).css('color', '');
            if (tempTxtEmployeeName == $('#' + txtEmployeeInputId).val()) {
                $('#' + txtEmployeeInputId).val('');
            }
        }).blur(function () {
            if (!$('#' + txtEmployeeInputId).val().replace(/[ ]/g, "")) {
                $('#' + txtEmployeeInputId).val(tempTxtEmployeeName).css('color', 'gray');
            }
        });
    }

    $('#' + txtDepartInputId).change(function () {
        if (txtDepartHidId) {
            $('#' + txtDepartHidId).val('');
        }
        if (txtEmployeeInputId) {
            $('#' + txtEmployeeInputId).val(txtEmployeeValue);
            if (txtEmployeeValue) {
                $('#' + txtEmployeeInputId).css('color', 'gray');
            }

        }
        if (txtEmployeeHidId) {
            $('#' + txtEmployeeHidId).val('');
        }
    });

    //部门智能提示
    $('#' + txtDepartInputId).autocomplete(getDepartUrl, {
        width: 260,
        max: 20,
        selectFirst: false,
        formatItem: function (row) {
            return row.DepartmentName;
        },
        formatResult: function (row) {
            return row.DepartmentName;
        }
    }).result(function (a, b) {
        if (txtDepartHidId) {
            $('#' + txtDepartHidId).val(b.DepartmentKeyId);
            $('#' + txtDepartInputId).blur();
        }

    });

    $('#' + txtEmployeeInputId).change(function () {
        if (txtEmployeeHidId) {
            $('#' + txtEmployeeHidId).val('');
        }
        if (isEmployeeLink) {
            $("#" + txtDepartHidId).val("");
        }
    });

    //员工智能提示
    //开启了点击出下拉框模式，最少输入字符设置为零
    $('#' + txtEmployeeInputId).autocomplete(getEmployeetUrl, {
        width: 260,
        max: 20,
        selectFirst: false,
        cacheLength: 0,
        clickMode: true,
        minChars:0,
        formatItem: function (row) {
            if (row.IsPublicAccount) {
                return row.UserName + "<span style='float:right'>" + row.DepartmentName + "</span>";
            } else {
                return row.UserName + "(" + row.EmployeeNo + ")<span style='float:right'>" + row.DepartmentName + "</span>";
            }
        },
        formatResult: function (row) {
            return row.UserName;
        }
    }).result(function (a, b) {
        if (txtEmployeeHidId) {
            $('#' + txtEmployeeInputId).blur();
            $('#' + txtEmployeeHidId).val(b.UserKeyId);
        }
        if (isDepartLink) {
            if (!$('#' + txtDepartHidId).val()) {
                $('#' + txtEmployeeInputId).blur();
                $('#' + txtDepartInputId).css('color', '');
                $('#' + txtDepartInputId).val(b.DepartmentName);
                $('#' + txtDepartHidId).val(b.DepartmentKeyId);
                $('#' + txtDepartInputId).removeClass("checkError").next(".wrong").remove();
            }

        }
        if (isEmployeeLink) {
            $('#' + txtDepartHidId).val(b.DepartmentKeyId);
        }
        if (ReturnFun) {
            ReturnFun(b);
        }

    });
};

//根据输入内容获得部门及员工智能提示返回部门Id及员工id
EmployeePrompt.getDepartAndEmployeeByInputExcCode = function (deptKeyId,txtDepartInputId, txtDepartHidId, txtEmployeeInputId, txtEmployeeHidId, exceptMe, hasPublicAccount, scope, txtEmployeeValue, txtDepartValue, isDepartLink, isEmployeeLink, DeparControlEmployee,isTransferUser, ReturnFun,ReturnFun2) {
    //赵贲改,选部门的时候,无需判断范围   

    var getDepartUrl = function () {
        //#spanTransferStaff .userDepatRoleCheck input[type=checkbox]
        if (isTransferUser != undefined) {
            return "/property/department-auto-match-ajax?name=" + escape($('#' + txtDepartInputId).val()) + '&ExceptMe=' + exceptMe + '&hasPublicAccount=true&scope=' + scope + "&keyId=" + $('#' + deptKeyId).val()+"&isTransferUser=true";
        }
        return "/property/department-auto-match-ajax?name=" + escape($('#' + txtDepartInputId).val()) + '&ExceptMe=' + exceptMe + '&hasPublicAccount=true&scope=' + scope + "&keyId=" + $('#' + deptKeyId).val();
    };
    var getEmployeetUrl = function () {
        if (DeparControlEmployee == undefined) {
            DeparControlEmployee = true;
        }
        if (txtDepartInputId && DeparControlEmployee) {
            return "/property/user-auto-complete-obtain-ajax?name=" + escape($('#' + txtEmployeeInputId).val()) + '&ExceptMe=' + exceptMe + '&hasPublicAccount=' + hasPublicAccount + '&DepartmentKeyId=' + $('#' + txtDepartHidId).val();
        } else {
            return "/property/user-auto-complete-obtain-ajax?name=" + escape($('#' + txtEmployeeInputId).val()) + '&ExceptMe=' + exceptMe + '&hasPublicAccount=' + hasPublicAccount;
        }
    };
    if (txtDepartValue) {
        var tempTxtDepartmentName = txtDepartValue;
        if ($('#' + txtDepartInputId).val() == tempTxtDepartmentName) {
            $('#' + txtDepartInputId).css('color', 'gray');
        }
        $('#' + txtDepartInputId).focus(function () {
            $('#' + txtDepartInputId).css('color', '');
            if (tempTxtDepartmentName == $('#' + txtDepartInputId).val()) {
                $('#' + txtDepartInputId).val('');
            }

        }).blur(function () {
            if (!$('#' + txtDepartInputId).val().replace(/[ ]/g, "")) {
                $('#' + txtDepartInputId).val(tempTxtDepartmentName).css('color', 'gray');
            }
        });
        var tempTxtEmployeeName = txtEmployeeValue;
        if ($('#' + txtEmployeeInputId).val() == tempTxtEmployeeName) {
            $('#' + txtEmployeeInputId).css('color', 'gray');
        }
        $('#' + txtEmployeeInputId).focus(function () {
            $('#' + txtEmployeeInputId).css('color', '');
            if (tempTxtEmployeeName == $('#' + txtEmployeeInputId).val()) {
                $('#' + txtEmployeeInputId).val('');
            }
        }).blur(function () {
            if (!$('#' + txtEmployeeInputId).val().replace(/[ ]/g, "")) {
                $('#' + txtEmployeeInputId).val(tempTxtEmployeeName).css('color', 'gray');
            }
        });
    }

    $('#' + txtDepartInputId).change(function () {
        if (txtDepartHidId) {
            $('#' + txtDepartHidId).val('');
        }
        if (txtEmployeeInputId) {
            $('#' + txtEmployeeInputId).val(txtEmployeeValue);
            if (txtEmployeeValue) {
                $('#' + txtEmployeeInputId).css('color', 'gray');
            }

        }
        if (txtEmployeeHidId) {
            $('#' + txtEmployeeHidId).val('');
        }
    });

    //部门智能提示
    $('#' + txtDepartInputId).autocomplete(getDepartUrl, {
        width: 260,
        max: 20,
        selectFirst: false,
        formatItem: function (row) {
            return row.DepartmentName;
        },
        formatResult: function (row) {
            return row.DepartmentName;
        }
    }).result(function (a, b) {
        if (txtDepartHidId) {
            $('#' + txtDepartHidId).val(b.DepartmentKeyId);
            $('#' + txtDepartInputId).blur();
            if (ReturnFun2) {
                ReturnFun2(b.DepartmentKeyId);
            }
        }

    });

    $('#' + txtEmployeeInputId).change(function () {
        if (txtEmployeeHidId) {
            $('#' + txtEmployeeHidId).val('');
        }
        if (isEmployeeLink) {
            $("#" + txtDepartHidId).val("");
        }
    });

    //员工智能提示
    //开启了点击出下拉框模式，最少输入字符设置为零
    $('#' + txtEmployeeInputId).autocomplete(getEmployeetUrl, {
        width: 260,
        max: 20,
        selectFirst: false,
        cacheLength: 0,
        clickMode: true,
        minChars: 0,
        formatItem: function (row) {
            if (row.IsPublicAccount) {
                return row.UserName + "<span style='float:right'>" + row.DepartmentName + "</span>";
            } else {
                return row.UserName + "(" + row.EmployeeNo + ")<span style='float:right'>" + row.DepartmentName + "</span>";
            }
        },
        formatResult: function (row) {
            return row.UserName;
        }
    }).result(function (a, b) {
        if (txtEmployeeHidId) {
            $('#' + txtEmployeeInputId).blur();
            $('#' + txtEmployeeHidId).val(b.UserKeyId);
        }
        if (isDepartLink) {
            if (!$('#' + txtDepartHidId).val()) {
                $('#' + txtEmployeeInputId).blur();
                $('#' + txtDepartInputId).css('color', '');
                $('#' + txtDepartInputId).val(b.DepartmentName);
                $('#' + txtDepartHidId).val(b.DepartmentKeyId);
                $('#' + txtDepartInputId).removeClass("checkError").next(".wrong").remove();
            }

        }
        if (isEmployeeLink) {
            $('#' + txtDepartHidId).val(b.DepartmentKeyId);
        }
        if (ReturnFun) {
            ReturnFun(b);
        }

    });
};
//新的部门员工联动智能提示控件
/*--
option.departmentSelector 部门文本框选择器以下选择器都最好唯一
option.departmentIDSelector 部门ID存储选择器
option.employeeSelector 员工文本框选择器
option.employeeIDSelector 员工ID存储选择器
option.exceptMe 员工选择器中，智能提示不包含当然登录用户 默认没有
option.hasPublicAccount 在员工中是否有公盘帐号有是public 没有是private 默认有
option.EmployeeDefault 员工文本框中默认显示的字样
option.departmentDefault 部门文本框中默认显示的字样
option.isAlone true 部门和员工没有任何联系，员工的选择范围不根据部门框决定，默认是否
option.employeeToDep 当选择员工后，是否将员工的部门更新到部门文本框里
option.departmentScope 部门选择的范围，默认1，全部，3本部，4本人
option.returnFun 选择员工后的自定义事件
option.departmentReturnFun 选择部门后的自定义事件
基本功能，如果不是独立模式，员工选择器会看部门选择器有没有值，如果有，则选择该部门的员工，没有，则全部员工，
当文本框变化时，对应的id也会清空,部门变化时，员工也会清空
--*/
EmployeePrompt.DepartmentAndEmployeeChoose = function (option) {
    var departmentSelector = $(option.departmentSelector);
    var employeeSelector = $(option.employeeSelector);
    var employeeIDSelector = $(option.employeeIDSelector);
    var departmentIDSelector = $(option.departmentIDSelector);
    var getDepartUrl = function () {
        if (!option.departmentScope) {
            option.departmentScope = 1;
        }
        return "/property/department-auto-match-ajax?name=" + escape(departmentSelector.val()) + '&hasPublicAccount=true&scope=' + option.departmentScope;
    };
    var getEmployeetUrl = function () {
       if (option.departmentSelector && $(option.departmentSelector).val() && !option.isAlone) {
            return "/property/user-auto-complete-obtain-ajax?name=" + escape($(option.employeeSelector).val()) + '&ExceptMe=' + option.exceptMe + '&hasPublicAccount=' + option.hasPublicAccount + '&DepartmentKeyId=' + $(option.departmentIDSelector).val();
        } else {
            return "/property/user-auto-complete-obtain-ajax?name=" + escape($(option.employeeSelector).val()) + '&ExceptMe=' + option.exceptMe + '&hasPublicAccount=' + option.hasPublicAccount;
        }
    };
    if (option.departmentSelector) {
        var tempTxtDepartmentName = option.departmentDefault;
        if (departmentSelector.val() == tempTxtDepartmentName) {
            departmentSelector.css('color', 'gray');
        }
        departmentSelector.focus(function () {
            departmentSelector.css('color', '');
            if (tempTxtDepartmentName == departmentSelector.val()) {
                departmentSelector.val('');
            }

        }).blur(function () {
            if (!$(departmentSelector).val().replace(/[ ]/g, "")) {
                $(departmentSelector).val(tempTxtDepartmentName).css('color', 'gray');
            }
        });

        departmentSelector.change(function () {
            if (option.departmentIDSelector) {
                departmentIDSelector.val('');
            }
            if (!option.isAlone) {
                if (option.employeeSelector) {
                    $(employeeSelector).val(option.EmployeeDefault);
                    if (option.EmployeeDefault) {
                        employeeSelector.css('color', 'gray');
                    }
                    employeeIDSelector.val('');
                }
            }
        });
    }
    if (option.employeeSelector) {
        var tempTxtEmployeeName = option.EmployeeDefault;
        if (employeeSelector.val() == tempTxtEmployeeName) {
            employeeSelector.css('color', 'gray');
        }
        employeeSelector.focus(function () {
            employeeSelector.css('color', '');
            if (tempTxtEmployeeName == employeeSelector.val()) {
                employeeSelector.val('');
            }
        }).blur(function () {
            if (!employeeSelector.val().replace(/[ ]/g, "")) {
                employeeSelector.val(tempTxtEmployeeName).css('color', 'gray');
            }
        });
        employeeSelector.change(function () {
            if (option.employeeIDSelector) {
                employeeIDSelector.val('');
            }
        });
    }
    //部门智能提示
    departmentSelector.autocomplete(getDepartUrl, {
        width: 260,
        max: 20,
        selectFirst: false,
        formatItem: function (row) {
            return row.DepartmentName;
        },
        formatResult: function (row) {
            return row.DepartmentName;
        }
    }).result(function (a, b) {
        departmentSelector.blur();
        departmentIDSelector.val(b.DepartmentKeyId);
        if (option.departmentReturnFun) {
            option.departmentReturnFun(b);
        }
    });

   

    //员工智能提示
    //开启了点击出下拉框模式，最少输入字符设置为零
    employeeSelector.autocomplete(getEmployeetUrl, {
        width: 260,
        max: 20,
        selectFirst: false,
        cacheLength: 0,
        clickMode: true,
        minChars: 0,
        formatItem: function (row) {
            return row.UserName + "(" + row.EmployeeNo + ")<span style='float:right'>" + row.DepartmentName + "</span>";
        },
        formatResult: function (row) {
            return row.UserName;
        }
    }).result(function (a, b) {
        employeeSelector.blur();
        employeeIDSelector.val(b.UserKeyId);
        if (option.employeeToDep) {
            if (!departmentIDSelector.val()) {
                departmentSelector.css('color', '');
                departmentSelector.val(b.DepartmentName);
                departmentIDSelector.val(b.DepartmentKeyId);
                departmentIDSelector.removeClass("checkError").next(".wrong").remove();
            }
        }
        if (option.returnFun) {
            option.returnFun(b);
        }
    });
}

//支持多部门智能提示,目前用于钥匙记录
EmployeePrompt.getDepartsByInput = function (txtDepartInputId, txtDepartValue) {
    var getDepartUrl = function () {
        var DepartName = $('#' + txtDepartInputId).val();
        return "/property/department-auto-match-ajax?name=" + escape(DepartName) + '&ExceptMe=false&hasPublicAccount=true&scope=1';
    };
    if (txtDepartValue) {
        var tempTxtDepartmentName = txtDepartValue;
        if ($('#' + txtDepartInputId).val() == tempTxtDepartmentName) {
            $('#' + txtDepartInputId).css('color', 'gray');
        }
        $('#' + txtDepartInputId).focus(function () {
            $('#' + txtDepartInputId).css('color', '');
            if (tempTxtDepartmentName == $('#' + txtDepartInputId).val()) {
                $('#' + txtDepartInputId).val('');
            }

        }).blur(function () {
            if (!$('#' + txtDepartInputId).val().replace(/[ ]/g, "")) {
                $('#' + txtDepartInputId).val(tempTxtDepartmentName).css('color', 'gray');
            }
        });
    }
    //部门智能提示
    $('#' + txtDepartInputId).autocomplete(getDepartUrl, {
        width: 260,
        max: 20,
        selectFirst: false,
        cacheLength: 0,
        formatItem: function (row) {
            return row.DepartmentName;
        },
        formatResult: function (row) {
            return row.DepartmentName;
        }
    });
}

//可以获取到锁定和离职的人员账号
EmployeePrompt.getAllDepartAndEmployeeByInput = function (employeeName, employeeKeyId, departmentName, departmentKeyId) {
    $(employeeName).autocomplete("/account/GetUserListBy?name=" + escape($("#txtUserName").val()), {
        width: 200,
        max: 20,
        selectFirst: false,
        formatItem: function (row) {
            return row.UserName + "(" + row.EmployeeNo + ")<span style='float:right'>" + row.DepartmentName + "</span>";
        },
        formatResult: function (row) {
            return row.UserName;
        }
    }).result(function (a, b) {
        $(employeeKeyId).val(b.UserKeyId);
        $(departmentKeyId).val(b.DepartmentKeyId);
        $(departmentName).val(b.DepartmentName);
    });
};
